package com.suanfa.sort.ti;

/**
 * @author jukg
 * @date 2025/01/06 14:35
 * @description
 * 给你一个有序数组 nums ，请你 原地 删除重复出现的元素，使得出现次数超过两次的元素只出现两次 ，返回删除后数组的新长度
 */
public class RemoveDuplicatesII {

    public static void main(String[] args) {
        int[] nums = {1,1,1,2,2,3};
        System.out.println(removeDuplicates(nums));

    }

    public static int removeDuplicates(int[] nums) {

        if(nums.length <2){
            return nums.length;
        }
        int slow = 2;
        int fast = 2;

        while(fast < nums.length){

           if(nums[slow-2] != nums[fast]){
               nums[slow] = nums[fast];
               slow++;
           }
           fast ++;
        }
        return slow;

    }
}
